package com.ohxwj.modules.web.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * Created by Administrator on 2016/8/6 0006.
 */
public class JacksonUtil {

    public static Object lock = new Object();

    public static ObjectMapper mapper = new ObjectMapper();

    public static <T> String marshallToString(T _T) {
        synchronized (lock) {
            try {
                return mapper.writeValueAsString(_T);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static <T> T jsonToObject(String json, Class<T> clazz) {
        try {
            return mapper.readValue(json, clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
